package com.example.shellshopping.YCB_settings;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.example.shellshopping.FPageFragment;
import com.example.shellshopping.MeFragment;
import com.example.shellshopping.R;
import com.example.shellshopping.TypeFragment;
import com.example.shellshopping.goods.GoodsTypeFragment;
import com.example.shellshopping.goods.TypeAdapter;
import com.example.shellshopping.goods.TypeBean;
import com.example.shellshopping.newLayout.HorizontalListView;
import com.example.shellshopping.utils.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class YCBAboutGuizeActivity extends BaseActivity {
    ImageView back;
    HorizontalListView typeLV;
    List<String> data;  //数据源
    private YCBAboutGuizeAdapter adapter;
    private FragmentManager fm;
    //将当前的activity包含的fragment放到一个集合里，便于管理
    List<Fragment>fragmentList;

    private void initView() {
        back = findViewById(R.id.ycb_about_guize_back);
        typeLV = findViewById(R.id.ycb_about_guize_hlv);
    }
    private void setListener() {
        back.setOnClickListener(onClickListener);

        typeLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setClickPos(position);
                changeRightPage(position);
            }
        });
    }
    //点击上侧的item切换右侧的页面
    private void changeRightPage(int postion){
        //TypeBean.DataBean dataBean = data.get(postion);  //得到切换的数据
        //int id = dataBean.getId();
        //FragmentTransaction transaction = fm.beginTransaction();
        //GoodsTypeFragment gtfrag = GoodsTypeFragment.newInstance(id + "", "");
        //transaction.replace(R.id.ycb_about_guize_layout,gtfrag);
        //transaction.commit();
        changeFragment(postion);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ycb_about_guize_back:
                    finish();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ycb_about_guize_activity);
        initView();
        //fm = getChildFragmentManager();
        initFragment();
        //确定数据源
        data = new ArrayList<>();
        //设置适配器
        adapter = new YCBAboutGuizeAdapter(this,data);
        typeLV.setAdapter(adapter);
        initData();
        adapter.notifyDataSetChanged();//提示更新
        changeRightPage(0);
        setListener();
    }

    private void initData() {
        data.add("协议规则");
        data.add("公告");
        data.add("评议院");
        data.add("普法专栏");
        data.add("违法违纪公告");
        //changeRightPage(0);
    }
    //初始化fragment管理集合
    private void initFragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new YCBGuizeOneFragment());
        fragmentList.add(new YCBGuizeTwoFragment());
        fragmentList.add(new YCBGuizeThreeFragment());
        fragmentList.add(new YCBGuizeFourFragment());
        fragmentList.add(new YCBGuizeFiveFragment());
        //将碎片添加到activity当中
        //1、获取碎片管理对象
        fm = getSupportFragmentManager();
        //2、获取碎片事物对象
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i=0;i<fragmentList.size();i++){
            transaction.add(R.id.ycb_about_guize_layout,fragmentList.get(i));
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(0));
        transaction.commit();
    }
    //切换Fragment
    private void changeFragment(int pos){
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i=0;i<fragmentList.size();i++){
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(pos));
        transaction.commit();
    }
}